package com.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LeetCode1408 {

    public List<String> stringMatching(String[] words) {

        List<String> wordList = new ArrayList<String>();
        List<String> result = new ArrayList<String>();

        for (String word : words) {
            wordList.add(word);
        }

        for (int i = 0; i < words.length; i++) {
            for (int j = i + 1; j < words.length; j++) {
                String iStr = wordList.get(i);
                String jStr = wordList.get(j);
                if (iStr.contains(jStr) || jStr.contains(iStr)) {
                    if (iStr.length() > jStr.length()) {
                        result.add(jStr);
                    }
                    if (iStr.length() < jStr.length()) {
                        result.add(iStr);
                    }
                }
            }
        }

        return result.stream().distinct().collect(Collectors.toList());
    }

    public static void main(String[] args) {
        LeetCode1408 demo2 = new LeetCode1408();
        String[] words = {"leetcoder","leetcode","od","hamlet","am"};
        List<String> strings = demo2.stringMatching(words);
        System.out.println(strings);
    }

}
